{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "抑制转义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c:\\windows\n",
      "c:\\windows\\\n"
     ]
    }
   ],
   "source": [
    "print(r'c:\\windows')\n",
    "print(r'c:\\windows' '\\\\') #若字符串末尾有\\,则上面方法出错,该方式可行."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "%格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=4, b=5.68\n",
      "how a\n"
     ]
    }
   ],
   "source": [
    "a=4\n",
    "b=5.678\n",
    "print('a=%d, b=%.2f' %(a,b))\n",
    "\n",
    "c = 'how are you'\n",
    "print('%.*s' % (5,c))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "{}格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a= 1.23\n",
      "world hello world\n",
      "数学：90, 计算机 80\n",
      "类的值为：5\n",
      "#123, 20 ,-3.14\n"
     ]
    }
   ],
   "source": [
    "print('{} {}'.format('a=',1.23))\n",
    "print('{1} {0} {1}'.format('hello', 'world'))\n",
    "\n",
    "score = {'math':90, 'computer':80}\n",
    "print('数学：{math}, 计算机 {computer}'.format(**score))\n",
    "\n",
    "class C:\n",
    "    def __init__(self,v):\n",
    "        self.v = v\n",
    "n = C(5)\n",
    "print('类的值为：{0.v}'.format(n))\n",
    "#>为右对齐，#为填充字符，^为居中对齐\n",
    "print('{:#>4d},{:^4o},{:+.2f}'.format(123,16,-3.14159)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "123 1.23\n",
      "a= 4\n",
      "a= 4\n"
     ]
    }
   ],
   "source": [
    "print(str(123)) \n",
    "print(int('123'),float('1.23'))\n",
    "print('a=',repr(a))\n",
    "print('a=',repr(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "增删"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcabcabcabc\n",
      "abcdef\n",
      "abc def\n",
      "w are you\n"
     ]
    }
   ],
   "source": [
    "print('abc' * 4)\n",
    "print('abc' + 'def')\n",
    "\n",
    "print(' \\tabc def  '.strip()) #删除两头空格,lstrip和rstrip分别去除左边和右边空格."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HOW ARE YOU\n"
     ]
    }
   ],
   "source": [
    "print(c.upper())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "How are you\n",
      "how 1r5 you\n",
      "my name is John\n",
      "my name is John\n"
     ]
    }
   ],
   "source": [
    "print(c.replace('how', 'How')) #替换\n",
    "\n",
    "table = str.maketrans('abcde' , '12345') #len(table)=256\n",
    "print(c.translate(table)) #按字母替换, 第二个参数表示需要删除的字符\n",
    "\n",
    "import string\n",
    "#美元符号后面为要替换的东西,也可以重写Template类\n",
    "template = string.Template('my name is $name')\n",
    "print(template.substitute(name='John'))\n",
    "d = {'name': 'John'}\n",
    "print(template.substitute(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "-1\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "print(c.find('are'))\n",
    "print(c.find('123'))\n",
    "print(c.find('are', 2, 8)) #提供起点和终点"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "How Are You\n",
      "HOW ARE YOU\n",
      "True\n",
      "True\n",
      "False False True True\n",
      "False True\n"
     ]
    }
   ],
   "source": [
    "print(c.count('o'))\n",
    "print(c.count('o', 3, 10))\n",
    "\n",
    "print(c.title())\n",
    "print(c.upper())\n",
    "print(c.islower())\n",
    "print(c.startswith('how'))\n",
    "\n",
    "print(' 123'.isdigit(), '4.56'.isdigit(), 'abc'.isalpha(), 'a2'.isalnum())\n",
    "print('a\\n'.isprintable(), ' \\n\\t\\r'.isspace())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其它"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['how', 'are', 'you']\n",
      "how are you\n",
      "0123456789\n",
      "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\n"
     ]
    }
   ],
   "source": [
    "l = c.split(' ') #默认以分隔符作为分隔\n",
    "\n",
    "print(l)\n",
    "print(' '.join(l)) #将列表连接\n",
    "\n",
    "print(string.digits)\n",
    "print(string.punctuation) #全部标点"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## bytes\n",
    "\n",
    "定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b''\n",
      "b'\\x00\\x00\\x00\\x00\\x00'\n",
      "<class 'bytes'>\n",
      "b'\\xb0\\xa1'\n",
      "b'abc'\n"
     ]
    }
   ],
   "source": [
    "print(bytes())\n",
    "a = bytes(5)\n",
    "print(a)\n",
    "print(type(a))\n",
    "print(b'\\xb0\\xa1')\n",
    "\n",
    "b = bytes([97,98,99])\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\xb0\\xa1'\n",
      "b'\\xb0\\xa1'\n",
      "abc\n"
     ]
    }
   ],
   "source": [
    "c = bytes('啊',encoding='gbk') #str转bytes\n",
    "print(c)\n",
    "print('啊'.encode('gbk')) #str转bytes\n",
    "print(b.decode('gbk')) #bytes转str"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其他"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bytearray(b'ac')\n",
      "65 A\n"
     ]
    }
   ],
   "source": [
    "d = bytearray([97,98,99]) #可变\n",
    "del d[1]\n",
    "print(d)\n",
    "print(ord('A'), chr(65))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
